package com.cyw.email.util;

import com.cyw.email.entity.DayEntity;
import com.cyw.email.service.DayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName 邮箱工具类
 * @Author: chenyw
 * @Date: 2022/01/25/16:45
 * @Version:
 */
@Component
public class EmailUtil {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${mail.fromMail.sender}")
    private String sender;

    @Value("${mail.fromMail.receiver}")
    private String receiver;

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 发送普通文本邮件
     */
    public String sendMail(String receiver,String content){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(sender);
        message.setTo(receiver);
        message.setSubject("cyw");
        message.setText(content);
        try {
            javaMailSender.send(message);
            logger.info("简单邮件已经发送。");
            return "success";
        } catch (Exception e) {
            logger.error("发送简单邮件时发生异常！", e);
            return "error";
        }
    }

    /**
     * 发送页面html邮件
     */
    public String semdHtmlMail(String receiver){
        DayEntity day = DayService.getDay();


        String content="<html>\n" +
                "<body style="+"background-color:pink"+">\n" +
                "<h1 style="+"color:pink;text-align:center"+">Hello 文杰宝贝,你好呀,ლ(°◕‵ƹ′◕ლ)</h1><br>"+
                "<h2 style="+"color:blue;text-align:left"+">Chen来报天气预报啦!</h2>"+
                "<h2 style="+"color:red;text-align:left"+">注意注意!!!当前时间:" +"</h2>"+
                "<h2 style="+"color:#f40;text-align:left"+"> "+new SimpleDateFormat( "yyyy年MM月dd日 E HH点mm分ss秒").format(new Date()) +"</h2><br>"+
                "<h2 style="+"color:green;text-align:left"+">今天"+day.getProvince()+day.getCity()+"的天气如下:</h2>"+
                "<h2 style="+"color:#ff6600;text-align:left"+">当前体感温度:"+day.getTemperature()+"℃</h2>"+
                "<h2 style="+"color:#9933ff;text-align:left"+">当前天气预测:"+day.getWeather()+"</h2>"+
                "<h2 style="+"color:#66cc33;text-align:left"+">当前风向预测:"+day.getWinddirection()+"</h2>"+
                "<h2 style="+"color:#0000ff;text-align:left"+">当前风力预测:"+day.getWindpower()+"级</h2>"+
                "<h2 style="+"color:#663366;text-align:left"+">当前空气湿度:"+day.getHumidity()+"</h2>"+
                "<h2 style="+"color:red;text-align:left"+">你自己多注意哦!!!</h2>"+
                "<h2 style="+"color:red;text-align:left"+">"+TimeUtil.differenceTime("2021-05-20 00:00:00", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())) +"</h2>"+
                "<h2 style="+"color:red;text-align:left"+">(ε￣ *)!!!（づ￣3￣）づ╭❤～</h2><br>"+
                "<h1 style="+"color:black;text-align:right"+">from--Love_you二叔</h1><br>"+
                "</body>\n" +
                "</html>";
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            //true表示需要创建一个multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sender);
            helper.setTo(receiver);
            helper.setSubject("html mail");
            helper.setText(content, true);

            javaMailSender.send(message);
            logger.info("html邮件发送成功");
        } catch (MessagingException e) {
            logger.error("发送html邮件时发生异常！", e);
        }
        return "success";
    }

    /**
     * 发送附件邮件
     */
    public String sendFilesMail(String receiver,String fileUrl){
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sender);
            helper.setTo(receiver);
            helper.setSubject("附件邮件");
            helper.setText("这是一封带附件的邮件", true);

            FileSystemResource file = new FileSystemResource(new File(fileUrl));
            String fileName = fileUrl.substring(fileUrl.lastIndexOf(File.separator));
            helper.addAttachment(fileName, file);

            javaMailSender.send(message);
            logger.info("带附件的邮件已经发送");
        } catch (MessagingException e) {
            logger.error("发送带附件的邮件时发生异常！", e);
        }
        return "success";
    }

}
